// convenience variables
var Assert = YAHOO.util.Assert;
var log = YAHOO.log;
var JSON = YAHOO.lang.JSON;
var sparqlns = "PREFIX sc: <http://purl.org/science/owl/sciencecommons/> ";
sparqlns += "PREFIX mesh: <http://purl.org/commons/record/mesh/> ";
sparqlns += "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " ;

var pause = 500;

function testEndpointHasData(testcase) {
	var query = "ASK { ?subject ?predicate ?object }";
	testAskQuery("pubmed", testcase, query, true, "Endpoint has no data.");
};

function testEndpointHasNCBIRecordForSpecificGene (testcase, gene, expected) {
	YAHOO.log("testEndpointHasNCBIRecordForSpecificGene: "+gene);	
	
	var query = "ASK from <http://purl.org/science/graph/ncbi/gene-info>" +
			"{ ?gene sc:ggp_has_symbol ?symbol . +" +
				"filter regex (?symbol, \""+ gene + "\")" +
			"}" ;
	
	var message = "There should be at least one ncib gene record for gene '"+gene+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasArticlessForSpecificGene(testcase, gene, expected) {
	YAHOO.log("testEndpointHasArticlessForSpecificGene: "+gene);
	
	
	var query = "ASK from <http://purl.org/science/graph/ncbi/gene-pubmed>" +
			"{ <http://purl.org/commons/record/ncbi_gene/" + gene + "> " +
					"sc:describes_gene_or_gene_product_mentioned_by " +
					"?pub .}";
	
	var message = "There should be at least one publication for gene '"+gene+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasGeneInfoForSpecificArticle(testcase, pub, expected) {
	YAHOO.log("testEndpointHasGeneInfoForSpecificArticle: "+pub);
	
	var query = "ASK from <http://purl.org/science/graph/ncbi/gene-pubmed>" +
			"{ ?gene sc:describes_gene_or_gene_product_mentioned_by " + pub + " .}" ;
	
	var message = "There should be at least one gene for publication '"+pub+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasRecordForSpecificArticle(testcase, article, expected) {
	YAHOO.log("testEndpointHasRecordForSpecificArticle: "+article);
	
	var recordID = article.replace(/science/, "commons");
	
	recordID = recordID.replace(/article/, "record");
		
	var query = "ASK from <http://purl.org/science/graph/medline/titles-years>" +
			"{ ?article sc:identified_by_pmid " + recordID + " .}" ;
	
	var message = "There should be at least one record for article '"+recordID+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasMinorMeshForSpecificPublicationRecord (testcase, pub, expected) {
	YAHOO.log("testEndpointHasMinorMeshForSpecificPublicationRecord: "+pub);
	
	//  much more expensive to retrieve the skos label of mesh terms
	
	var query = "ASK from <http://purl.org/science/graph/medline/subject-headings> " +
				"from <http://purl.org/science/graph/mesh/mesh-skos> " +
			"{ " + pub + " sc:has-as-minor-mesh ?mesh." +
				"?mesh skos:prefLabel ?name." +
				"}" ;
	
	var message = "There should be at least one minor mesh term for publication '"+pub+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasMajorMeshForSpecificPublicationRecord (testcase, pub, expected) {
	YAHOO.log("testEndpointHasMajorMeshForSpecificPublicationRecord: "+pub);
	
	var query = "ASK from <http://purl.org/science/graph/medline/subject-headings>" +
			"{ " + pub + " sc:has-as-major-mesh ?mesh.}" ;
	
	var message = "There should be at least one minor mesh term for publication '"+pub+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};

function testEndpointHasMeshForSpecificPublicationRecord (testcase, pub, expected) {
	YAHOO.log("testEndpointHasMeshForSpecificPublicationRecord: "+pub);
	
	var query = "ASK from <http://purl.org/science/graph/medline/subject-headings>" +
			"{ " + pub + " ?p ?term .}" ;
	
	var message = "There should be at least one mesh term for publication '"+pub+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};


function testEndpointHasBibForSpecificPublicationRecord (testcase, pub, expected) {
	YAHOO.log("testEndpointHasBibForSpecificPublication: "+pub);
	
	var query = "ASK from <http://purl.org/science/graph/medline/titles-years>" +
			"{ ?article sc:identified_by_pmid " + pub + "; " + 
					    "dc:title ?title ; dc:date ?date.}" ;
	
	var message = "There should be some bibliographic information for publication '"+pub+"'.";
	testAskQuery("pubmed", testcase, query, expected, message);
};


var pubmedTestCase = new YAHOO.tool.TestCase({
	name: "Pubmed SPARQL Endpoint at DERI Test Case",
	
	testEndpointHasData : function() {
        var tc = this;
		tc.wait(function() {testEndpointHasData(tc);}, pause);
	},
	
	testEndpointHasNCBIRecordForSpecificGene : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasNCBIRecordForSpecificGene(tc,"CG18350", true);}, pause);
		
	},
	
	testEndpointHasArticlessForSpecificGene : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasArticlessForSpecificGene(tc,"3772180", true);}, pause);
	},
	
	testEndpointHasGeneInfoForSpecificArticle : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasGeneInfoForSpecificArticle(tc,"<http://purl.org/science/article/pmid/7789765>", true);}, pause);
	},
	
	testEndpointHasRecordForSpecificArticle : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasRecordForSpecificArticle(tc,"<http://purl.org/science/article/pmid/7789765>", true);}, pause);
	},
	
	testEndpointHasMinorMeshForSpecificPublicationRecord : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasMinorMeshForSpecificPublicationRecord(tc,"<http://purl.org/commons/record/pmid/7789765>", true);}, pause);
	},
	
	testEndpointHasMajorMeshForSpecificPublicationRecord : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasMajorMeshForSpecificPublicationRecord(tc,"<http://purl.org/commons/record/pmid/7789765>", true);}, pause);
	},
	
	testEndpointHasMeshForSpecificPublicationRecord : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasMeshForSpecificPublicationRecord(tc,"<http://purl.org/commons/record/pmid/7789765>", true);}, pause);
	},
	
	testEndpointHasBibForSpecificPublicationRecord : function() {
		var tc = this;
		tc.wait(function() {testEndpointHasBibForSpecificPublicationRecord(tc,"<http://purl.org/commons/record/pmid/7789765>", true);}, pause);
	}
});

function setupLogger() {
    var loggerConfig = {
        thresholdMax : 100000,
        thresholdMin : 100000
    };
    var loggerContainer = document.getElementById("logger");
    var testLogger = new YAHOO.tool.TestLogger(loggerContainer, loggerConfig);
//	var logReader = new YAHOO.tool.TestLogger("logger");
	YAHOO.log("Logger setup done");	
};

function runTests() {
	YAHOO.log("Running tests");
	YAHOO.tool.TestRunner.add(pubmedTestCase);
	YAHOO.tool.TestRunner.run();
};

YAHOO.util.Event.onDOMReady(setupLogger);
YAHOO.util.Event.onDOMReady(runTests);
